/**
 * @fileOverview 组件基类
 * @author goodzsq@gmail.com
 */

var BaseClass = gg.module('BaseObject');
module.exports = gg.createClass({
    attributes: {
        css: {
            label: '样式',
            type: 'datatype.Object',
            setter: function(v) {
                this.el.removeClass(this.get('css'));
                this.defaultSetter('css', v);
                el.addClass(v);
            }
        },
        position: {
            label: '位置',
            type: 'datatype.Position',
            setter: function(v) {
                this.el.css({left: v.x, top: v.y});
            },
            getter: function() {
                var p = this.el.position();
                return {x: p.left, y: p.top};
            }
        },
        size: {
            label: '尺寸',
            type: 'datatype.Size',
            setter: function(v) {
                this.el.width(v.width).height(v.height);
            },
            getter: function() {
                return {width: this.el.width(), height: this.el.height()};
            }
        }
    },
    methods: {
        /**
         * 创建dom元素
         * @param {Object} attrs 属性
         * return {Dom}
         */
        _buildHtml: function(attrs) {
            attrs = attrs || {};
            var tag = attrs.tag || 'div';
            var el = $('<' + tag + '>');
            _.each(attrs, function(v, k) {
                if (k === 'tag') {
                    //skip
                } else if (k === 'html') {
                    el.html(v);
                } else if (k === 'text') {
                    el.text(v);
                } else if (k === 'val') {
                    el.val(v);
                } else {
                    el.attr(k, v);
                }
            });
            return el;
        },
        /**
         * 创建html元素
         */
        createElement: function() {
            var css = 'ui';
            this.el = this._buildHtml({id: this.cid, class: css});
        },
        /**
         * @override
         */
        beforeInit: function() {
            this.createElement();
        },
        /**
         * @override
         */
        afterInit: function() {
            if (typeof this.refresh === 'function') {
                this.refresh();
            }
        },
        /**
         * 销毁元素
         */
        destroy: function() {
            BaseClass.prototype.destroy.call(this);
            this.el.remove();
            delete this.el;
        }
    },
    events: {
    }
}, BaseClass, []);
